/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.vasthu.core.dao;

import com.vasthu.api.dao.PurchasedItemDAO;
import com.vasthu.api.domain.PurchasedItem;
import com.vasthu.api.domain.Stock;
import com.vasthu.utility.VstDBManager;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import org.apache.log4j.Logger;

/**
 *
 * @author moremada
 */
public class PurchasedItemDAOImpl implements PurchasedItemDAO {

    static Logger logger = Logger.getLogger(PurchasedItemDAOImpl.class);

    @Override
    public boolean insert(PurchasedItem purItem, long billId) {
        if (purItem == null) {
            return false;
        }
        PreparedStatement pst = null;
        Connection con = VstDBManager.get().getConnection();
        try {
            pst = con.prepareStatement("INSERT INTO stock_bill(quantity, discount, sub_total, bill_id, stock_id) "
                    + "VALUES(?, ?, ?, ?, ?)");

            pst.setFloat(1, purItem.getQuantity());
            pst.setFloat(2, purItem.getDiscountRate());
            pst.setFloat(3, purItem.getSubTotal());
            pst.setLong(4, billId);
            pst.setLong(5, purItem.getStockItem().getStockId());
            pst.executeUpdate();

        } catch (Exception ex) {
            logger.error("unable to insert data to stok_bill : " + ex);
        } finally {
            VstDBManager.get().close();
        }
        return true;
    }

    @Override
    public List<PurchasedItem> getPurchasedItems(long billId) {
        List<PurchasedItem> items = new ArrayList<PurchasedItem>();
        PreparedStatement pst = null;
        ResultSet rs = null;
        Connection con = VstDBManager.get().getConnection();
        try {
            pst = con.prepareStatement("SELECT * FROM stock_bill WHERE bill_id = ?");
            pst.setFloat(1, billId);
            rs = pst.executeQuery();
            while (rs.next()) {
                PurchasedItem pItem = new PurchasedItem();
                pItem.setDiscountRate(rs.getFloat("discount"));
                pItem.setQuantity(rs.getFloat("quantity"));
                pItem.setSubTotal(rs.getFloat("sub_total"));
                Stock stock = new Stock();
                stock.setStockId(rs.getInt("stock_id"));
                pItem.setStockItem(stock);
                items.add(pItem);
            }
        } catch (SQLException ex) {
            logger.error("Unable to load purchased items for the given bill Id " + ex);
        } finally {
            VstDBManager.get().close();
        }
        return items;
    }

    @Override
    public boolean update(PurchasedItem purItem, long billId) {
        if (purItem == null) {
            return false;
        }
        PreparedStatement pst = null;
        Connection con = VstDBManager.get().getConnection();
        try {
            pst = con.prepareStatement("UPDATE stock_bill SET quantity = ? , sub_total = ? WHERE item_code = ? AND bill_id = ?");

            pst.setFloat(1, purItem.getQuantity());
            pst.setFloat(2, purItem.getSubTotal());
            pst.setString(3, purItem.getStockItem().getItemCode());
            pst.setLong(4, billId);
            pst.executeUpdate();

        } catch (Exception ex) {
            logger.error("unable to update stok_bill : " + ex);
        } finally {
            VstDBManager.get().close();
        }
        return true;
    }

    @Override
    public boolean delete(PurchasedItem purItem, long billId) {
        PreparedStatement pst = null;
        Connection con = VstDBManager.get().getConnection();
        try {
            pst = con.prepareStatement("DELETE FROM stok_bill WHERE item_code = ? AND bill_id = ?");
            pst.setString(1, purItem.getStockItem().getItemCode());
            pst.setLong(2, billId);
            pst.executeUpdate();
        } catch (Exception ex) {
            logger.error("Unable to delete from stok_bill : " + ex);
        } finally {
            VstDBManager.get().close();
        }

        return true;
    }
}
